<?php

class AppDefault extends navCtrl{
	
	/**
	 * 
	 * @param Request $req
	 * @param TimeMachine $delorean
	 * @param PDIO $pdio
	 */
	public function __construct($req, $delorean, $pdio) {
		
		global $DataTypes;
		parent::__construct($req, $delorean, $pdio);
		
	}
	
	
	public function index() {
		
		$auth = $this->plugins['Auth'];
		
		if (!$auth->logged()) {
				
			$form = $this->mainForm();
			$form->readRequest($this->request);
			
			$this->getError($errorMsg, true);
	
			$this->response->tpl('AppDefault/DefaultIndex.html');
			$form->writeResponse($this->response);
		} else {
			$user = $this->getUser($auth);
			$this->response->nomeutente($user->getName());
			$this->response->logged(true);
			$this->response->tpl('AppDefault/DefaultIndex.html');
		}
		
		$this->response->pageTitle('Benvenudo');

	}
	
	
	public function registrati() {
		$auth = $this->plugins['Auth'];
		$form = $this->mainForm();
		$form->readRequest($this->request);
		$validation = $form->validate();
		if (!$validation->result()) {
			$this->goBack("not valid fields <br>".print_r($validation->errors(), true));
		} else {
			$this->response->set('testo', "validation ok");
		}

		$res = $auth->register($this->request->get('username'), $this->request->get('password'));
		$this->delorean->setRecord(false);
		
		if ($res) {
			$reg = 'OK';
		} else {
			$reg = 'fallita';
		}
		$this->response->set('resreg', $reg);
		$this->response->setTpl('AppDefault/resInterrogazione.html');
		
		
	}
	
	public function interrogazione() {
		$auth = $this->plugins['Auth'];
		$user = $this->getUser($auth);
		
		if ($auth->logged()) {
		    $this->response->set('logged', true);
		}
		
		$this->response->set('testo', 'ciao '.$user->getName());
		$this->response->setTpl('AppDefault/resInterrogazione.html');
		
	
	}	
	
	public function login() {
		
		$auth = $this->plugins['Auth'];
		$this->getError($errorMsg, true);
		
		$this->delorean->setRecord(false);
		
		$form = $this->mainForm();
		$form->readRequest($this->request);
		$validation = $form->validate();
		
		
		if (!$validation->result()) {
		    $this->goRefresh("not valid fields:<br>".print_r($validation->errors(), true));
		} else {
			$this->response->set('testo', "validation ok");
		}
		
		$msg = "login ok";
		
		try {	
    		$res = $auth->login($this->request->get('username'), $this->request->get('password'));
    			if (!$res) {
    				$msg = 'Dati Login Errati';
    			} else {
    				$msg = "login ok";
    			}		
		
		} catch (WrongLoginException $wle) {
			$msg = $wle->getMessage();
		}

		$this->reloadRefresh($msg);
			
	}
	
	
	public function terzoPasso() {
		
		$auth = $this->plugins['Auth'];
		$user = $this->getUser($auth);	

		$this->response->set('nomeutente', $user->getName());
		$this->response->setTpl('AppDefault/terzo.html');		
		
	}
	
	public function getUser($auth) {
	    
	    require_once APP_PATH.'classes/User.php';
	
	    if (!$auth->logged()) {
	        return new AnonymusUser();
	    } else {
	        return new LoggedUser($_COOKIE[COOKUSER]);
	    }
	
	}	
	
	
	
	private function mainForm()  {
		
		$form = new FormHandler();
		
		$form->addField('username', DataTypes::ALFANUMERICO, true);
		$form->addField('password', DataTypes::ALFANUMERICO, true);
		
		return $form;
		
	}
	
	
	public function prove() {
	    include_model("ProveDao");
	    
	    $pdao = new ProveDao($this->pdio);
	    
	    echoa($pdao->prova());die;
	    
	}
	
}


?>
